package com.cote.douhahou.pretloan.any.util

import android.app.Application
import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.cote.douhahou.pretloan.beans.PamGoogleAakomapalendInstall
import com.cote.douhahou.pretloan.any.http.AkomapalendHttpHelper
import com.cote.douhahou.pretloan.any.timezone.AkomapalendTimeZoneUtil
import com.cote.douhahou.pretloan.launcher.BaseContext
import com.cote.douhahou.pretloan.url.AkomapalendPaths
import java.sql.DriverManager.println

object AkomapalendGoogleInstallUtil {

    private var mInstallReferrer: String = ""

    private var mInstallReferrerClient: InstallReferrerClient =
        InstallReferrerClient.newBuilder(BaseContext.instance()).build()

    fun init(context: Application, success: (String) -> Unit) {
        try {
            if (mInstallReferrer.isBlank()) {
                mInstallReferrerClient.startConnection(object :
                    InstallReferrerStateListener {
                    override fun onInstallReferrerSetupFinished(responseCode: Int) {
                        when (responseCode) {
                            InstallReferrerClient.InstallReferrerResponse.OK -> {
                                try {
                                    println("google install call back")
                                    val response = mInstallReferrerClient.installReferrer
                                    mInstallReferrer = response?.installReferrer ?: ""
                                    mInstallReferrerClient.endConnection()

                                    success.invoke(mInstallReferrer)

                                } catch (e: Exception) {
                                    e.printStackTrace()
                                    mInstallReferrerClient.endConnection()
                                }
                            }
                            InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED -> {

                                println("FEATURE_NOT_SUPPORTED")
                                mInstallReferrerClient.endConnection()
                            }
                            InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE -> {

                                println("SERVICE_UNAVAILABLE")
                                mInstallReferrerClient.endConnection()
                            }
                        }
                    }

                    override fun onInstallReferrerServiceDisconnected() {
                    }
                })
            } else {
                success.invoke(mInstallReferrer)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun installReferrer(): String {
        return mInstallReferrer
    }

    fun saveS2sInfo(targetUrlList: List<String>) {
        val params =
            PamGoogleAakomapalendInstall(
                channelCode = installReferrer(),
                subChannelCode = installReferrer()
            )
        val body = params.toEncryptParams(AkomapalendPaths.PATH_GP_INSTALL_TEMP)
        val resultTargetUrlList = AkomapalendTimeZoneUtil.covertToRealTargetServers(targetUrlList)
        kotlin.io.println("====== $resultTargetUrlList")

        
        resultTargetUrlList.forEach { path ->
            AkomapalendHttpHelper.postData(path, body)
        }
    }
}